package com.chushouya.controller.common;

import com.chushouya.common.constant.ContentType;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.manager.dto.admin.im.Content;
import com.chushouya.manager.dto.admin.im.Event;
import com.chushouya.manager.dto.admin.im.Message;
import com.chushouya.manager.service.websocket.IMWebSocketServer;
import com.general.framework.core.lang.Strings;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "通讯调试")
@RestController
@RequestMapping(ServiceType.COMMON + "/websocket/")
@Slf4j
public class WebSocketController {

    /**
     * 群发消息内容1
     */
    @PostMapping("/broadCastMessage")
    public String broadCastMessage(String data){
        try {
            Content content = new Content();
            content.setType(ContentType.text.value());
            content.setData(data);
            IMWebSocketServer.broadCast(content);
        } catch (Exception e) {
            log.error("broadCastMessage exception", e);
        }
        return "ok";
    }

    /**
     * 群发消息内容2
     */
    @PostMapping("/broadCastMessageAsClientIds")
    public String broadCastMessageAsClientIds(String data, String prefix, String clientIds){
        try {
            Content content = new Content();
            content.setType(ContentType.text.value());
            content.setData(data);
            IMWebSocketServer.broadCast(content, prefix, Strings.splitToLongList(clientIds, ","));
        } catch (Exception e) {
            log.error("broadCastMessageAsClientIds exception", e);
        }
        return "ok";
    }

    /**
     * 指定会话ID发消息
     * @param data 消息内容
     */
    @PostMapping("/sendOneMessage")
    public String sendOneMessage(String fromClientId, String toClientId, String data){
        try {
            Message message = new Message();
            message.setFrom(fromClientId);
            message.setTo(toClientId);
            message.setEvent(Event.messagePush.value());
            Content content = new Content();
            content.setType(ContentType.text.value());
            content.setData(data);
            message.setContent(content);
            IMWebSocketServer.sendOne(message);
        } catch (Exception e) {
            log.error("sendOneMessage exception", e);
        }
        return "ok";
    }
    /**
     * 服务端请求关闭
     * @param clientId 客户端ID
     */
    @PostMapping("/requestClose")
    public String requestClose(String clientId){
        try {
            IMWebSocketServer.requestClose(clientId);
        } catch (Exception e) {
            log.error("requestClose exception", e);
        }
        return "ok";
    }
    /**
     * 服务端请求关闭
     */
    @PostMapping("/broadCastRequestClose")
    public String broadCastRequestClose(){
        try {
            IMWebSocketServer.SESSION_MAPS.keySet().forEach(IMWebSocketServer::requestClose);
        } catch (Exception e) {
            log.error("broadCastRequestClose exception", e);
        }
        return "ok";
    }
}
